package com.vivz.baby;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.vivz.baby.task.AddLogTask;

public class LoggingActivity extends Activity {

    private static final String TAG = "LoggingActivity";

    private static final int RADIO_SELECTION_LB = 0;
    private static final int RADIO_SELECTION_RB = 1;
    private static final int RADIO_SELECTION_FM = 2;
    private static final int RADIO_SELECTION_BT = 3;
    private static final SparseIntArray FEEDING_VALUES = new SparseIntArray();

    private static final String RADIO_SELECTION_NO = "NO";
    private static final String RADIO_SELECTION_WT = "WT";
    private static final String RADIO_SELECTION_DT = "DT";
    private static final String RADIO_SELECTION_WD = "WD";

    // Static initialization
    private static int feedingSelection = RADIO_SELECTION_LB;
    private static String diapersSelection = RADIO_SELECTION_NO;

    private SeekBar seekBarFeedingValue;
    private Button buttonLogData;
    private Button buttonReset;
    private TextView textViewFeedingValue;
    private ProgressBar savingProgressBar;

    private RadioButton radioLB;
    private RadioButton radioRB;
    private RadioButton radioFM;
    private RadioButton radioBT;

    private RadioButton radioNO;
    private RadioButton radioWT;
    private RadioButton radioDT;
    private RadioButton radioWD;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logging);
        initializeUI();
        initializeData();
    }

    // Instance initialization
    private void initializeData() {
        Log.i(TAG, "initializeData");
        feedingSelection = RADIO_SELECTION_LB;
        diapersSelection = RADIO_SELECTION_NO;

        FEEDING_VALUES.clear();
        FEEDING_VALUES.put(RADIO_SELECTION_LB, 0);
        FEEDING_VALUES.put(RADIO_SELECTION_RB, 0);
        FEEDING_VALUES.put(RADIO_SELECTION_FM, 0);
        FEEDING_VALUES.put(RADIO_SELECTION_BT, 0);

        setSeekBarScale();
    }

    private void initializeUI() {
        Log.i(TAG, "initializeUI");

        initializeUISeekBar();

        initializeUIFeedingValueTextView();

        initializeUIFeedingRadioButtons();

        initializeUIDiapersRadioButtons();

        initializeUIButtons();

        initializeUIStates();
        
        initializeUIProgessBar();
    }

    private void initializeUIProgessBar() {
    	savingProgressBar = (ProgressBar) findViewById(R.id.progress_saving_id);
		savingProgressBar.setVisibility(View.INVISIBLE);
	}
    
	private void initializeUISeekBar() {
        seekBarFeedingValue = (SeekBar) findViewById(R.id.feeding_seek_bar_id);
        seekBarFeedingValue.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    FEEDING_VALUES.put(feedingSelection, progress);
                }
                updateFeedingValueText();
            }
        });
    }

    private void initializeUIFeedingValueTextView() {
        textViewFeedingValue = (TextView) findViewById(R.id.text_feeding_value_id);
    }

    private void initializeUIFeedingRadioButtons() {
        radioLB = (RadioButton) findViewById(R.id.radio_btn_left_breast_id);
        radioRB = (RadioButton) findViewById(R.id.radio_btn_right_breast_id);
        radioFM = (RadioButton) findViewById(R.id.radio_btn_formula_id);
        radioBT = (RadioButton) findViewById(R.id.radio_btn_bottle_id);

        radioLB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                feedingSelection = RADIO_SELECTION_LB;
                setSeekBarScale();
                updateFeedingValueText();
            }
        });

        radioRB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                feedingSelection = RADIO_SELECTION_RB;
                setSeekBarScale();
                updateFeedingValueText();
            }
        });
        radioFM.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                feedingSelection = RADIO_SELECTION_FM;
                setSeekBarScale();
                updateFeedingValueText();
            }
        });
        radioBT.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                feedingSelection = RADIO_SELECTION_BT;
                setSeekBarScale();
                updateFeedingValueText();
            }
        });
    }

    private void initializeUIDiapersRadioButtons() {
        radioNO = (RadioButton) findViewById(R.id.radio_btn_diapers_no_id);
        radioWT = (RadioButton) findViewById(R.id.radio_btn_diapers_wt_id);
        radioDT = (RadioButton) findViewById(R.id.radio_btn_diapers_dt_id);
        radioWD = (RadioButton) findViewById(R.id.radio_btn_diapers_wd_id);

        radioNO.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                diapersSelection = RADIO_SELECTION_NO;
            }
        });
        radioWT.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                diapersSelection = RADIO_SELECTION_WT;
            }
        });
        radioDT.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                diapersSelection = RADIO_SELECTION_DT;
            }
        });
        radioWD.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                diapersSelection = RADIO_SELECTION_WD;
            }
        });
    }

    private void initializeUIButtons() {

        buttonLogData = (Button) findViewById(R.id.button_log_id);
        buttonLogData.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            	savingProgressBar.setVisibility(View.VISIBLE);
            	
                // Set the url
                final String url = "" + //
                        getString(R.string.url_aws_domain) + //
                        getString(R.string.url_services_context) + //
                        getString(R.string.partial_url_log);

                // Build the Log object to be saved
                Calendar instance = Calendar.getInstance();
                String user = "Amruta [" + DateFormat.format("MMM dd", instance) + "]";
                com.vivz.baby.persistence.model.Log log = new com.vivz.baby.persistence.model.Log(user);

                log.setLeftBreast(FEEDING_VALUES.get(RADIO_SELECTION_LB));
                log.setRightBreast(FEEDING_VALUES.get(RADIO_SELECTION_RB));
                log.setFormula(FEEDING_VALUES.get(RADIO_SELECTION_FM));
                log.setPumped(FEEDING_VALUES.get(RADIO_SELECTION_BT));
                log.setDiaper(diapersSelection);
                Date now = instance.getTime();
                log.setTimestamp(now);
                log.setUpdateTimestamp(now);

                // Call the Async task to persist the log
                Log.i(TAG, "... before calling async task ...");
                new AddLogTask().execute(url, log);
                Log.i(TAG, "... after calling async task ...");
                savingProgressBar.setVisibility(View.INVISIBLE);
                finish();
            }
        });

        buttonReset = (Button) findViewById(R.id.button_reset_id);
        buttonReset.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                initializeData();
                initializeUIStates();
            }
        });
    }

    private void initializeUIStates() {
        Log.i(TAG, "initializeStates");
        radioLB.setChecked(true);
        radioNO.setChecked(true);
        updateFeedingValueText();
    }

    private void updateFeedingValueText() {

        String text = String.valueOf(FEEDING_VALUES.get(feedingSelection));

        switch (feedingSelection) {
            case RADIO_SELECTION_LB:
            case RADIO_SELECTION_RB:
                text += ' ' + getString(R.string.str_feeding_minutes);
                break;
            case RADIO_SELECTION_FM:
            case RADIO_SELECTION_BT:
                text += ' ' + getString(R.string.str_feeding_millis);
                break;
        }

        textViewFeedingValue.setText(text);
    }

    private void setSeekBarScale() {
        Log.i(TAG, "setSeekBarScale");
        switch (feedingSelection) {
            case RADIO_SELECTION_LB:
            case RADIO_SELECTION_RB:
                seekBarFeedingValue.setMax(60);
                seekBarFeedingValue.setKeyProgressIncrement(1);
                break;

            case RADIO_SELECTION_FM:
            case RADIO_SELECTION_BT:
                seekBarFeedingValue.setMax(200);
                seekBarFeedingValue.setKeyProgressIncrement(5);
                break;
        }

        seekBarFeedingValue.setProgress(FEEDING_VALUES.get(feedingSelection));
    }

}